package com.zl.customer.service.impl;/**
 * @author suke
 * @create 2021-07-09 11:52
 */

import com.zl.customer.exception.CustomerException;
import com.zl.customer.mapper.CustomerMapper;
import com.zl.customer.mapper.UserinfoMapper;
import com.zl.customer.pojo.Customer;
import com.zl.customer.pojo.CustomerCondition;
import com.zl.customer.pojo.Page;
import com.zl.customer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 * @ClassName: CutomserServiceImpl
 * @author: suke
 * @since: 2021/7/9 11:52
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    public  CustomerMapper customerMapper;
    @Autowired
    private UserinfoMapper userinfoMapper;

    @Override
    public List<Customer> queryAll() throws Exception {
        return  customerMapper.queryAll(); //sqlSession.selectList()
    }

    @Override
    public int addCustomer(Customer customer) throws Exception {
        return  customerMapper.insert(customer);
    }

    @Override
    public int deleteById(Integer id) throws Exception{
        if(id == null){
            throw new CustomerException("删除客户时,需要选中id");
        }
        return customerMapper.delete(id);
    }

    @Override
    public Customer queryOne(int id) throws Exception {
        return customerMapper.queryById(id);
    }

    @Override
    public Page<Customer> queryByPage(int pageIndex, int pageSize) throws Exception {
        Page<Customer> page = new Page<>();
        CustomerCondition condition = new CustomerCondition();
        condition.setPageIndex(pageIndex);
        condition.setPageSize(pageSize);
        return queryCondition(condition);



    }

    @Override
    public Page<Customer> queryCondition(CustomerCondition condition) throws Exception {
        //创建一个Page对象
        Page<Customer> page = new Page<>();
        page.setPageIndex(condition.getPageIndex());
        page.setPageSize(condition.getPageSize());
        //总记录数
        page.setCount(customerMapper.count(condition));
        //当前页记录
        page.setDatas(customerMapper.queryByPage(condition));
        return page;
    }
}
